package com.hpc.Service;

import entity.article;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
public class MyScheduledService2 {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private com.hpc.mapper.saveMapper saveMapper;
    @Scheduled(cron = "*/3 * * * * *")//每3秒执行一次将redis中的用户关注数据持久化进mysql表followOfUser
    public void followsOfUserSave(){
        try {
            Set<String> keys = redisTemplate.keys("*_follow");
            if (keys.size()==0){
                return;
            }else {
                saveMapper.followsOfUserTruncate();
                for (String key : keys) {
                    String userId=key.substring(0,key.indexOf("_"));
                    List<String> range = redisTemplate.opsForList().range(key, 0, -1);
                    if (range.size()==0){
                        return;
                    }else {
                        for (String s : range) {
                            saveMapper.followsOfUserSave(UUID.randomUUID().toString(),userId,s);
                        }
//                        System.out.println("持久化成功-followsOfUserSave！");
                    }
                }
            }
        }catch (Exception e){
            System.out.println("执行错误-personallikeOfUserSave!"+e.getMessage());
        }
    }
    @Scheduled(cron = "*/3 * * * * *")//每3秒执行一次将redis中的个人喜欢数据持久化数据库表personallikeOfUser
    public void personallikeOfUserSave(){
        try {
            Set<String> keys = redisTemplate.keys("*_personallike");
            if (keys.size()==0){
                return;
            }else {
                saveMapper.personallikeOfUserTruncate();
                for (String key : keys) {
                    String userId=key.substring(0,key.indexOf("_"));
                    List<article> range = redisTemplate.opsForList().range(key, 0, -1);
                    if (range.size()==0){
                        return;
                    }else {
                        for (article article : range) {
                            saveMapper.personallikeOfUserSave(UUID.randomUUID().toString(),userId,article.getSerial());
                        }
//                        System.out.println("持久化成功-personallikeOfUserSave！");
                    }
                }
            }
        }catch (Exception e){
            System.out.println("持久化失败-personallikeOfUserSave！"+e.getMessage());
        }
    }
    @Scheduled(cron = "*/3 * * * * *")//每3秒执行一次将redis中的用户消息持久化进mysql数据库表msgOfUser
    public void msgOfUserSave(){
        try {
            Set<String> keys = redisTemplate.keys("*_msg");
            if (keys.size()==0){
                return;
            }else {
                saveMapper.msgOfUserTruncate();
                for (String key : keys) {
                    String userId=key.substring(0,key.indexOf("_"));
                    List<String> range = redisTemplate.opsForList().range(key, 0, -1);
                    for (String s : range) {
                        saveMapper.msgOfUserSave(UUID.randomUUID().toString(),userId,s);
                    }
                    redisTemplate.delete(key);
                }
//                System.out.println("持久化成功-msgOfUserSave!");
            }
        }catch (Exception e){
            System.out.println("持久化失败-msgOfUserSave！"+e.getMessage());
        }
    }
}
